/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cinema.client.cinemaclient.ui;

import com.cinema.client.cinemaclient.managers.MovieManager;
import com.cinema.client.cinemaclient.model.Movie;
import com.cinema.client.cinemaclient.model.User;
import com.cinema.client.cinemaclient.tablemodels.MovieTableModel;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author Zsolt Szecsi
 */
public class MovieFrame extends JFrame {
    
    private JPanel mainPanel;
    private JScrollPane moviesScrollPane;
    private JTable movieTable;
    private MovieManager movieManager;
    private JButton profileBtn, addMovieBtn, deleteMovieBtn, updateMovieBtn, refreshBtn, showActorsBtn;
    private User user;
    private MovieTableModel movieTableModel;
    
    public MovieFrame(User user) {
        super("Movies");
        // init simple attributes
        movieManager = new MovieManager();
        this.user = user;
        // init ui elements
        mainPanel = new JPanel();
        JPanel buttonPanel = new JPanel();
        profileBtn = new JButton("My Profile");
        addMovieBtn = new JButton("Add new movie");
        deleteMovieBtn = new JButton("Delete movie");
        updateMovieBtn = new JButton("Update movie");
        refreshBtn = new JButton("Refresh");
        showActorsBtn = new JButton("Schow actors");
        // Compose ui
        mainPanel.setLayout(new BorderLayout());
        buttonPanel.setLayout(new GridLayout(1, 6));
        buttonPanel.add(profileBtn);
        buttonPanel.add(addMovieBtn);
        buttonPanel.add(deleteMovieBtn);
        buttonPanel.add(updateMovieBtn);
        buttonPanel.add(refreshBtn);
        buttonPanel.add(showActorsBtn);
        
        if ("user".equals(user.getRole())) {
            addMovieBtn.setVisible(false);
            deleteMovieBtn.setVisible(false);
            updateMovieBtn.setVisible(false);
        }
        
        mainPanel.add(buttonPanel, BorderLayout.SOUTH);
        
        loadMovieTable();
        setListeners();
        
        this.setContentPane(mainPanel);
        this.setContentPane(mainPanel);
        this.setBounds(100, 100, 500, 300);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    private void loadMovieTable() {
        List<Movie> movies = movieManager.getAllMovies();
        
        movieTableModel = new MovieTableModel(movies);
        
        movieTable = new JTable(movieTableModel);
        
        moviesScrollPane = new JScrollPane(movieTable);
        mainPanel.add(moviesScrollPane, BorderLayout.CENTER);
    }
    
    private void setListeners() {
        profileBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                ProfileFrame profileFrame = new ProfileFrame(MovieFrame.this, user);
                profileFrame.setVisible(true);
                MovieFrame.this.setVisible(false);
            }
        });
        addMovieBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                MovieDetailFrame movieDetailFrame = new MovieDetailFrame(MovieFrame.this, null);
                movieDetailFrame.setVisible(true);
                MovieFrame.this.setVisible(false);
            }
        });
        updateMovieBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int selectedRow = movieTable.getSelectedRow();
                if (selectedRow > -1) {
                    Movie selectedMovie = movieManager.getMovieById((Integer) movieTableModel.getValueAt(selectedRow, 0));
                    
                    MovieDetailFrame movieDetailFrame = new MovieDetailFrame(MovieFrame.this, selectedMovie);
                    movieDetailFrame.setVisible(true);
                    MovieFrame.this.setVisible(false);
                } else {
                    JOptionPane.showMessageDialog(MovieFrame.this, "Error: Please select a row before start the update process!");
                }
            }
        });
        refreshBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                refresh();
            }
        });
        deleteMovieBtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int selectedRow = movieTable.getSelectedRow();
                if (selectedRow > -1) {
                    movieManager.deleteMovie((Integer) movieTableModel.getValueAt(selectedRow, 0));
                    refresh();
                } else {
                    JOptionPane.showMessageDialog(MovieFrame.this, "Error: Please select a row before start the update process!");
                }
            }
        });
        
        showActorsBtn.addMouseListener(new MouseAdapter() {
            
            @Override
            public void mouseClicked(MouseEvent e) {
                ActorsFrame actorsFrame = new ActorsFrame();
                actorsFrame.setVisible(true);
            }
        });
        
    }
    
    public void refresh() {
        List<Movie> movies = movieManager.getAllMovies();
        movieTableModel.setMovies(movies);
        movieTableModel.fireTableDataChanged();
    }
    
}
